package com.flyqiu.flow.core.manage;


import com.flyqiu.flow.api.script.FlyQiuFlowScriptActuator;
import com.flyqiu.flow.api.script.FlyQiuFlowScriptActuatorManage;
import com.flyqiu.flow.api.script.IVariableScope;
import com.flyqiu.common.tools.StringTools;

import java.util.HashMap;
import java.util.Map;

public class DefaultFlyQiuFlowScriptActuatorManage implements FlyQiuFlowScriptActuatorManage {
    private final Map<String, FlyQiuFlowScriptActuator> actuatorMap = new HashMap<>();

    private String scriptActuatorType;

    @Override
    public void register(FlyQiuFlowScriptActuator flyQiuFlowScriptActuator) {
        if (StringTools.isEmpty(flyQiuFlowScriptActuator.scriptType())) {
            throw new RuntimeException("flyQiuFlowScriptActuator.scriptType  cannot be null or empty");
        }
        actuatorMap.put(flyQiuFlowScriptActuator.scriptType(), flyQiuFlowScriptActuator);
    }

    @Override
    public String getDefaultScriptActuatorType() {
        return scriptActuatorType;
    }

    @Override
    public void setDefaultScriptActuatorType(String scriptActuatorType) {
        this.scriptActuatorType = scriptActuatorType;
    }




    @Override
    public Object execute(String scriptType, String script, IVariableScope variableScope) {
        FlyQiuFlowScriptActuator flyQiuFlowScriptActuator = getScriptActuator(scriptType);
        return flyQiuFlowScriptActuator.actuatorResult(script, variableScope);
    }

    public FlyQiuFlowScriptActuator getScriptActuator(String scriptType) {
        if (!actuatorMap.containsKey(scriptType)) {
            throw new RuntimeException("No executor found for script type " + scriptType);
        }
        return actuatorMap.get(scriptType);
    }
}
